//
//  basicInfo.swift
//  万才信息App
//
//  Created by 陌上互联  on 16/7/14.
//  Copyright © 2016年 万才信息. All rights reserved.
//

import Foundation

//基本信息接口
class basicInfo{
    
    private static let ASMX_NAME="BasicInfoManageInterface.asmx"
    
//    获取简历信息常量数据接口
    private static let GET_RESUME_ENUM="GetResumeEnum"
//    获取省份数据
    private static let GET_PROVINCE="GetProvince"
//    根据省份id获取城市数据
    private static let GET_CITY_BY_ID="GetCityList"
//    获取公司性质的接口
    private static let GET_COMPANY_ENUM="GetCompanyEnum"
//    根据用户id获取用户基本信息
    private static let GET_USER_INFO_BY_ID="GetUserBasicInfoByUserId"
//    根据用户id修改用户基本信息
    private static let UPDATA_USER_INFO="UpdateUserBasicInfo"
//    上传头像接口
    private static let UPDATE_IMAGE="UpLoadLogo"
//    修改密码
    private static let UPDATE_PASSWORD="UpdatePassword"
//    获取行业信息数据接口
    private static let GET_INDUSTRY="GetIndustry"
//    获取职位大类
    private static let GET_JOB="GetJobFunction"
//    获取职位小类
    private static let GET_JOB_BY_PARENT="GetJobFunctionByParentId"
    
    
//    获取职位大类
    static func getJobType(successCallback:(JSON)->Void,
                              failCallback:(String)->Void){
        let mysoap=soapUtil()
        
        mysoap.setName(ASMX_NAME, methodName: GET_JOB)
        
        mysoap.send({ (data) in
            successCallback(data)
        }) { (error) in
            failCallback("获取职位大类常量失败")
        }
    }
//    获取职位小类
    static func getJobByParent(parentID:NSNumber,
                              successCallback:(JSON)->Void,
                              failCallback:(String)->Void){
        let mysoap=soapUtil()
        
        mysoap.setName(ASMX_NAME, methodName: GET_JOB_BY_PARENT)
        
        mysoap.addParam("parentFuncId", value: parentID.stringValue)
        
        mysoap.send({ (data) in
            successCallback(data)
        }) { (error) in
            failCallback("获取职位小类常量失败")
        }
    }


//    获取简历信息常量
    static func getResumeEnum(enumID:NSNumber,
                                      successCallback:(JSON)->Void,
                                      failCallback:(String)->Void){
        let mysoap=soapUtil()
        
        mysoap.setName(ASMX_NAME, methodName: GET_RESUME_ENUM)
        
        mysoap.addParam("enumTypeId", value: enumID.stringValue)
        
        mysoap.send({ (data) in
            successCallback(data)
            }) { (error) in
                failCallback("获取简历常量失败")
        }
    }
    
//    获取省份数据
    static func getProvince(successCallback:(JSON)->Void,
                                    failCallback:(String)->Void){
        let mysoap=soapUtil()
        
        mysoap.setName(ASMX_NAME, methodName: GET_PROVINCE)
        
        mysoap.send({ (data) in
            successCallback(data)
        }) { (error) in
            failCallback("获取省份失败")
        }
    }
    
//    根据省份id获取城市数据
    static func getCity(parentId:String,successCallback:(JSON)->Void,
                                failCallback:(String)->Void){
        let mysoap=soapUtil()
        
        mysoap.setName(ASMX_NAME, methodName: GET_CITY_BY_ID)
        
        mysoap.addParam("parentId", value: parentId)
        
        mysoap.send({ (data) in
            successCallback(data)
        }) { (error) in
            failCallback("获取城市数据失败")
        }
    }
    
//    获取公司性质
    static func getCompanyNature(successCallback:(JSON)->Void,
                          failCallback:(String)->Void){
        let mysoap=soapUtil()
        
        mysoap.setName(ASMX_NAME, methodName: GET_COMPANY_ENUM)
        
        mysoap.addParam("enumTypeId", value: "1")
        
        mysoap.send({ (data) in
            successCallback(data)
        }) { (error) in
            failCallback("获取公司性质常量失败")
        }
    }
    
//    获取公司规模
    static func getCompanyScale(successCallback:(JSON)->Void,
                                        failCallback:(String)->Void){
        let mysoap=soapUtil()
        
        mysoap.setName(ASMX_NAME, methodName: GET_COMPANY_ENUM)
        
        mysoap.addParam("enumTypeId", value: "2")
        
        mysoap.send({ (data) in
            successCallback(data)
        }) { (error) in
            failCallback("获取公司规模常量失败")
        }
    }
    
//      获取用户基本信息
    static func getUserInfoByID(userId:String,successCallback:(JSON)->Void,
                                        failCallback:(String)->Void){
        let mysoap=soapUtil()
        
        mysoap.setName(ASMX_NAME, methodName: GET_USER_INFO_BY_ID)
        
        mysoap.addParam("userId", value: userId)
        
        mysoap.send({ (data) in
            if let dataArray=data["rows"].array{
                if dataArray.count >= 1{
                    successCallback(dataArray[0])
                }else{
                    successCallback(nil)
                }
            }else{
                failCallback("获取用户基本信息失败")
            }
        }) { (error) in
            failCallback("获取用户基本信息失败")
        }
    }
    
//    获取行业信息
    static func getIndustry(successCallback:(JSON)->Void,
                            failCallback:(String)->Void){
        let mysoap=soapUtil()
        
        mysoap.setName(ASMX_NAME, methodName: GET_INDUSTRY)
        
        mysoap.send({ (data) in
            successCallback(data)
        }) { (error) in
            failCallback("获取行业信息失败")
        }
    }
    
    
//      修改用户基本信息
    static func changeUserInfoByID(userId:String,
                                        userName:String,
                                        gender:Int,
                                        marriage:Int,
                                        birthday:NSDate,
                                        degree:Int,
                                        workyear:Int,
                                        mobile:String,
                                        email:String,
                                        location:Int,
                                        successCallback:(JSON)->Void,
                                        failCallback:(String)->Void){
        let mysoap=soapUtil()
        
        mysoap.setName(ASMX_NAME, methodName: UPDATA_USER_INFO)
        
        mysoap.addParam("userId", value: userId)
//        各种参数设置：
//        用户名
        mysoap.addParam("userName", value: userName)
//        性别（1男2女）
        mysoap.addParam("gender", value: String(gender))
//        是否已婚（1已婚2未婚）
        mysoap.addParam("marriage", value: String(marriage))
//        出生日期 TODO DateTime类型 格式2010-05-24T18:13:00
        let DateFormatter=NSDateFormatter()
        DateFormatter.dateFormat="yyyy-MM-ddTHH:mm:ss"
        let convertDate=DateFormatter.stringFromDate(birthday)
        mysoap.addParam("birthday", value: convertDate)
//        最高学历
        mysoap.addParam("degree", value: String(degree))
//        工作年限
        mysoap.addParam("workyear", value: String(workyear))
//        手机号码
        mysoap.addParam("mobile", value: mobile)
//        电子邮箱
        mysoap.addParam("email", value: email)
//        现居地
        mysoap.addParam("location", value: String(location))
        
        
        mysoap.send({ (data) in
            if data["result"] != nil{
                if(data["result"].string == "true"){
                    successCallback(data)
                }else{
                    print("enter here 1")
                    failCallback(data["msg"].string!)
                }
            }else{
                print("enter here 2")
                failCallback("获取用户基本信息失败")
            }
        }) { (error) in
            print("enter here 3")
            failCallback("获取用户基本信息失败")
        }
    }
    
//    上传头像接口 
//    TODO 这个接口有问题，先不做
    
//    更改密码接口
    static func changePassword(userName:String,oldpwd:String,newpwd:String,
                                       successCallback:(JSON)->Void,
                                       failCallback:(String)->Void
                                       ){
        let mysoap=soapUtil()
        
        mysoap.setName(ASMX_NAME, methodName: UPDATE_PASSWORD)
        
        mysoap.addParam("userName", value: userName)
        mysoap.addParam("oldpwd", value: oldpwd)
        mysoap.addParam("newpwd", value: newpwd)
        
        mysoap.send({ (data) in
//            TODO解析成功失败
            if let result=data["result"].string {
               if result=="true"{
//                  成功
                    print(data["msg"].string)
                    successCallback(data)
               }else{
//                  失败
                    print("更改密码失败")
                    failCallback("更改密码失败")
                }
            }
        }) { (error) in
            failCallback("更改密码失败")
        }
    }
}